package com.pirestupppai.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseAdapter;
import com.pirestupppai.base.BaseViewHolder;
import com.pirestupppai.bean.TupaiMsg;
import com.pirestupppai.common.Constant;
import com.pirestupppai.ui.activity.DetailActivity;
import com.pirestupppai.utils.ImageLoadUtil;
import com.pirestupppai.widgets.view.AvatarView;

import java.util.List;

import static com.pirestupppai.bean.TupaiMsg.CommentCommentedMessage;
import static com.pirestupppai.bean.TupaiMsg.FollowedMessage;
import static com.pirestupppai.bean.TupaiMsg.PostCommentedMessage;
import static com.pirestupppai.bean.TupaiMsg.PostLikedMessage;
import static com.pirestupppai.bean.TupaiMsg.PostRepliedMessage;

/**
 * 消息列表
 *
 * author: xiaoluo
 * date: 2017/5/25 10:46
 */
public class MessageListAdapter extends BaseAdapter<TupaiMsg> {
    private Context mContext;
    private List<TupaiMsg> mList;


    public MessageListAdapter(Context context, List<TupaiMsg> list) {
        super(list);
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_msg_list;
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        ImageView msgImage = holder.getView(R.id.msg_image);
        AvatarView msgAvatar = holder.getView(R.id.msg_avatar);
        TextView msgNickname = holder.getView(R.id.msg_nickname);
        TextView msgDesc = holder.getView(R.id.msg_desc);
        TextView msgTime = holder.getView(R.id.msg_time);
        TextView msgType = holder.getView(R.id.msg_type_tv);

        final TupaiMsg msg = mList.get(position);
        ImageLoadUtil.getInstance().loadAvatar(msg.getTarget_user().getAvatar(), msgAvatar);
        msgAvatar.setUid(msg.getTarget_user().getId(), false);
        msgNickname.setText(msg.getTarget_user().getNickname());
        msgTime.setText(msg.getTpTime());
        if (msg.getTarget_post() != null && msg.getTarget_post().getMedias() != null && msg.getTarget_post().getMedias().size() != 0) {
            ImageLoadUtil.getInstance().loadImage(msg.getTarget_post().getMedias().get(0).getPathname(), msgImage, ImageLoadUtil.LoadOption.LOAD_SMALL);
        }

        switch (msg.getType()) {
            // 帖子被评论
            case PostCommentedMessage:
                msgDesc.setVisibility(View.VISIBLE);
                msgImage.setVisibility(View.VISIBLE);
                msgType.setText("评论了我动态");
                msgDesc.setText(msg.getContent());
                break;
            // 评论被评论
            case CommentCommentedMessage:
                msgDesc.setVisibility(View.VISIBLE);
                msgImage.setVisibility(View.VISIBLE);
                msgType.setText("回复了我评论");
                msgDesc.setText(msg.getContent());
                break;
            // 帖子被点赞
            case PostLikedMessage:
                msgDesc.setVisibility(View.GONE);
                msgImage.setVisibility(View.VISIBLE);
                msgType.setText("赞了我");
                break;
            // 帖子有作品发表
            case PostRepliedMessage:
                msgDesc.setVisibility(View.VISIBLE);
                msgImage.setVisibility(View.VISIBLE);
                msgDesc.setText(msg.getContent());
                msgType.setText("回应了一张作品");
                break;
            // 被关注
            case FollowedMessage:
                msgDesc.setVisibility(View.GONE);
                msgImage.setVisibility(View.GONE);
                msgType.setText("关注了我");
                break;
        }

        if (msg.getType() != FollowedMessage) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, DetailActivity.class);
                    intent.putExtra(Constant.IntentKey.ID, msg.getTarget_post().getId());
                    mContext.startActivity(intent);
                }
            });
        } else {
            holder.itemView.setOnClickListener(null);
        }
    }

    private View.OnClickListener jumpListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(mContext, DetailActivity.class);
            intent.putExtra(Constant.IntentKey.ID, (int) v.getTag());
            mContext.startActivity(intent);
        }
    };
}
